package com.yhy.gateway.exception;

import com.yhy.common.core.result.Result;
import com.yhy.common.core.result.ResultEnum;
import com.yhy.gateway.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 无权访问自定义响应
 */
@Slf4j
@Component
public class ServerAccessDeniedHandler implements org.springframework.security.web.server.authorization.ServerAccessDeniedHandler {

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
        log.warn("未授权 {} :{}", e.getLocalizedMessage(), e);
        return ResponseUtils.writeErrorInfo(exchange, HttpStatus.UNAUTHORIZED, e, Result.fail(ResultEnum.ACCESS_UNAUTHORIZED));
    }
}
